package cn.chenxiejia;

import java.io.*;

/***
 * @program: maven
 * @ClassName: File11
 * @Description:
 * @Author: chenxiejia
 * @Date: 2022/1/19 20:03
 * @version : V1.0
 */
public class File11 {
    public static int num = 0;
    public static int num1 = 0;

    public static void main(String[] args) {
        count(new File("D:\\jdk-17.0.1\\lib\\src"));
        // txtnumber(new File("F:/jdk_src.txt"));
        System.out.println(num);
        System.out.println(num1);

    }

    public static void count(File file) {
        for (File t : file.listFiles()) {
            if (t.isDirectory()) {
                count(t);
            } else {
                isjava(t);
            }
        }
    }

    public static void isjava(File file) {
        if (file.exists()) {
            if (file.getName().endsWith(".java")) {
                num++;
                //try (var fis = new FileInputStream(file);
//                     var fis1 = new FileInputStream(file);
//                     var fos = new FileOutputStream(new File("F:/jdk17_src.txt"), true)) {
//                    fos.write(String.format("%n-----------------------------------------------------").getBytes());
//                    fos.write(String.format("%n文件名：%s", file.getName()).getBytes());
//                    fos.write(String.format("%n文件路径：%s", file.getAbsolutePath()).getBytes());
//                    fos.write(String.format("%n-----------------------------------------------------%n").getBytes());
//                    fis.transferTo(fos);


                try (var br = new BufferedReader(new FileReader(file));
                     var bw = new BufferedWriter(new FileWriter("F:/jdk17_src.txt", true))) {
                    bw.write(String.format("%n-----------------------------------------------------"));
                    bw.write(String.format("%n文件名：%s", file.getName()));
                    bw.write(String.format("%n文件路径：%s", file.getAbsolutePath()));
                    bw.write(String.format("%n-----------------------------------------------------%n"));
                    while (br.ready()) {
                        num1++;
                        bw.write(String.format("%n") + br.readLine());
                    }
                    bw.write(String.format("%n%n"));
                } catch (IOException e) {
                    e.printStackTrace();
                }

//                try (OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("F:/jdk1.txt", true), StandardCharsets.UTF_8);
//                     BufferedReader fis = new BufferedReader(new FileReader(file))) {
//                    osw.append(String.format("%n-----------------------------------------------------"));
//                    osw.append(String.format("%n文件名：%s", file.getName()));
//                    osw.append(String.format("%n文件路径：%s", file.getAbsolutePath()));
//                    osw.append(String.format("%n-----------------------------------------------------%n"));
//                    while (fis.ready()) {
//                        osw.append(fis.readLine());
//                    }
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
            }
        }
    }

//    public static void txtnumber(File file) {
//        try (var br = new BufferedReader(new FileReader(file));
//             var bw = new BufferedWriter(new FileWriter("F:/jdk.txt", true))) {
//            int a = 0;
//            while (br.ready()) {
//                bw.write(String.format("%4d%s", ++a, br.readLine()));
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
}
